package com.example.controller;

import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author lbq
 * @version 1.0
 * @date 2021/3/13 16:36
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册接口
     *
     * @param email    邮箱地址
     * @param password 密码
     * @param code     验证码
     * @return
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public String register(@PathVariable("email") String email,
                           @PathVariable("password") String password,
                           @PathVariable("code") String code,
                           HttpServletResponse response) throws Exception {
        return userService.register(email, password, code, response);
    }

    /**
     * 根据邮箱地址判断是否已注册
     *
     * @param email 邮箱地址
     * @return
     */
    @GetMapping("/isRegister/{email}")
    public String isRegister(@PathVariable("email") String email) {
        return userService.isRegister(email);
    }

    /**
     * 登录接口
     *
     * @param email    邮箱地址
     * @param password 密码
     * @return
     */
    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable("email") String email,
                        @PathVariable("password") String password,
                        HttpServletResponse response) throws Exception {
        return userService.login(email, password, response);
    }

    /**
     * 根据token查询用户登录邮箱
     *
     * @param token token
     * @return
     */
    @GetMapping("/info/{token}")
    public String info(@PathVariable("token") String token) {
        return userService.info(token);
    }
}
